import 'dart:convert';

import 'package:flutter/material.dart';
import '../../global/Global.dart';
import '../../services/ScreenAdapter.dart';
import '../../services/TLSizeFit.dart';
import '../../util/LogUtil.dart';
import '../../util/Storage.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../YColors.dart';

class PersonPage extends StatefulWidget {
  PersonPage({Key key}) : super(key: key);

  _PersonPageState createState() => _PersonPageState();
}

class _PersonPageState extends State<PersonPage> {


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    LogUtil.v("initState: 个人");
    getStorageData();
  }

  getStorageData() async {
    var strStor = await Storage.getString(Global.LOGINDATA);
    LogUtil.init(title: "来自LogUtil", isDebug: false, limitLength: 800);
    LogUtil.v("有缓存strStor:$strStor");


    var logBean = json.decode(strStor); // Json 字符串转化成 Map 类型
    var email = logBean["email"];
    LogUtil.v("email:$email");
    var phone = logBean["phone"];
    LogUtil.v("phone:$phone");
    var access_token = logBean["access_token"];
//    LogUtil.v("access_token:$access_token");

  }


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    TLSizeFit.initialize();
    ScreenAdapter.init(context);
    LogUtil.v("build 个人");
//    return SingleChildScrollView(
//      // 滑动
//      child: FilmContent(),
//    );
    return FilmContent();
  }

  Widget FilmContent() {
//    return Container(
//      color: YColors.t, // 设置背景色
    return SingleChildScrollView(
        child: Container(
            color: YColors.t,
//            color: Color(0xffF2F1F6),
            // 设置背景色
            width: double.infinity,
//            margin: EdgeInsets.only(top: TLSizeFit.setPx(60)),
            padding: EdgeInsets.fromLTRB(0, TLSizeFit.setPx(60), 0, 0),
            child: Column(
              children: <Widget>[
//                  Text("个人中心的数据"),
//                  Text("个人中心的数据"),
//                  Text("个人中心的数据"),

                Row(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.fromLTRB(
                          TLSizeFit.setPx(20), 0, TLSizeFit.setPx(20), 0),
                      width: TLSizeFit.setPx(68),
                      height: TLSizeFit.setPx(68),
//                      color: YColors.l,
                      alignment: Alignment.center,
                      child: Text(
                        "纪来",
                        style: TextStyle(
                            color: YColors.t, fontSize: ScreenAdapter.size(28)),
                      ),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(6),
                        color: YColors.l,
                      ),
                    ),
                    SizedBox(
                      width: TLSizeFit.setPx(10),
                    ),
                    Expanded(
                        flex: 1,
                        child: Container(
                          height: TLSizeFit.setPx(72),
//                          color: YColors.h,
                          margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "展纪来",
                                style: TextStyle(
                                    color: YColors.a,
                                    fontSize: ScreenAdapter.size(39)),
                              ),
                              SizedBox(
                                height: TLSizeFit.setPx(4),
                              ),
                              Text(
                                "北京神州光大科技有限公司",
                                style: TextStyle(
                                    color: YColors.b,
                                    fontSize: ScreenAdapter.size(28)),
                              ),
                              SizedBox(
                                height: TLSizeFit.setPx(4),
                              ),
                              Text(
                                '高级认证',
                                style: TextStyle(
                                  color: YColors.d,
                                  fontSize: ScreenAdapter.size(24),
                                  backgroundColor: Color(0xffF2F2F6),
                                ),
                              )
                            ],
                          ),
                        )),
                    Image.asset(
                      'images/user_home_arrow.png',
                      width: TLSizeFit.setPx(18),
                      height: TLSizeFit.setPx(18),
                    ),
                  ],
                ),

                SizedBox(
                  height: TLSizeFit.setPx(26),
                ),
                Divider(
                  height: 2,
                  endIndent: 15,
                  indent: 15,
                ),

                SizedBox(
                  height: TLSizeFit.setPx(18),
                ),

                Container(
                  margin: EdgeInsets.fromLTRB(
                      TLSizeFit.setPx(20), 0,TLSizeFit.setPx(20),0),
                  child:  Row(
                    children: <Widget>[
                      Image.asset(
                        "images/takephoto.png",
                        width: TLSizeFit.setPx(16),
                        height: TLSizeFit.setPx(16),
                      ),
                      SizedBox(
                        width: TLSizeFit.setPx(6),
                      ),
                      Text("添加状态"),
                    ],
                  ),

                ),
                SizedBox(
                  height: TLSizeFit.setPx(18),
                ),
                Container(
                  height: TLSizeFit.setPx(6),
                  color: Color(0xffF0F0F0),
                ),

                Container(
                  margin: EdgeInsets.fromLTRB(TLSizeFit.setPx(20), 0, TLSizeFit.setPx(20), 0),
                  height: TLSizeFit.setPx(54),
                  child: Row(
                    children: <Widget>[
                      Image.asset(
                        "images/personcenter1.png",
                        width: TLSizeFit.setPx(26),
                        height: TLSizeFit.setPx(26),
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Expanded(
                          flex: 1,
                          child: Text(
                            "钱包(支付宝)",
                            style: TextStyle(fontSize: ScreenAdapter.size(30)),
                          )),
                      Image.asset(
                        'images/user_home_arrow.png',
                        width: TLSizeFit.setPx(18),
                        height: TLSizeFit.setPx(18),
                      ),
                    ],
                  ),
                ),
                Container(
                  height: TLSizeFit.setPx(6),
                  color: Color(0xffF0F0F0),
                ),

                setItemView("images/personcenter1.png", "收藏"),

                Divider(
                  height: 10,
                  endIndent: 15,
                  indent: 15,
                  color: Color(0xffF0F0F0),
                ),

                setItemView("images/personcenter1.png", "看看"),
                Divider(
                  height: 1,
                ),
                setItemView("images/personcenter1.png", "运动"),
                Container(
                  height: TLSizeFit.setPx(6),
                  color: Color(0xffF0F0F0),
                ),
                setItemView("images/personcenter1.png", "客服与帮助"),
                Container(
                  height: TLSizeFit.setPx(6),
                  color: Color(0xffF0F0F0),
                ),
                setItemView("images/personcenter1.png", "发现"),
                Container(
                  height: TLSizeFit.setPx(6),
                  color: Color(0xffF0F0F0),
                ),

                InkWell(
                  child:  setItemView("images/personcenter1.png", "设置"),
                  onTap: (){

                    Navigator.pushNamed(context, "/SettingPage");
                  },
                ),

//                Container(
//                  width: 10,
//                  height: 10,
//                  child: setWebViewStorage(),
//                )


//                Container(
//                  height: TLSizeFit.setPx(40),
//                  color: Color(0xffF2F1F6),
//                ),
              ],
            )));
//    );
  }

  Widget setWebViewStorage(){
    return WebView(
//    initialUrl: url,
      //JS执行模式 是否允许JS执行
      onPageStarted: (String url) {
        print('onPageStarted开始: $url');
      },
      javascriptMode: JavascriptMode.unrestricted,
      onWebViewCreated: (WebViewController controller) {
//            _controller.clearCache();  // 去掉缓存
        setlocalStorage(controller);
        print("添加缓存去吧");
      },
      onPageFinished: (url) {
      },
//      initialUrl: url,
    );
  }

  /*写入token到localStorage*/
//    原文链接：https://blog.csdn.net/Emperor_huanzi/article/details/109218249
  setlocalStorage(controller) async{  //

    String access_token = await Storage.getToken();  //
    access_token = access_token.substring(7,access_token.length);
//    String access_token ="123333";
//    var loginData = await Storage.getString(Global.LOGINDATA);
    print("设置缓存。。。access_token:$access_token");

    if (access_token != null) {
      print("设置缓存。。。");
      controller.evaluateJavascript("window.localStorage.setItem('SZGDBoss3-token','$access_token');");
//      _controller.evaluateJavascript("window.localStorage.setItem('userInfo','$loginData');");  // 用户信息
    }
  }

  Widget setItemView(imagePath, str) {
    return Container(
      margin: EdgeInsets.fromLTRB(TLSizeFit.setPx(20), 0, TLSizeFit.setPx(20), 0),
      height: TLSizeFit.setPx(54),
      child: Row(
        children: <Widget>[
          Image.asset(
            imagePath,
            width: TLSizeFit.setPx(26),
            height: TLSizeFit.setPx(26),
          ),
          SizedBox(
            width: 10,
          ),
          Expanded(
              flex: 1,
              child: Text(
                str,
                style: TextStyle(fontSize: ScreenAdapter.size(30)),
              )),
          Image.asset(
            'images/user_home_arrow.png',
            width: TLSizeFit.setPx(18),
            height: TLSizeFit.setPx(18),
          ),
        ],
      ),
    );
  }
}
